#!/bin/bash

sh_home=/opt/app/sh

source $sh_home/account.conf

# 上传文件
# arg-1:文件全路径, arg-2:日期目录%Y/%d/%d
_put_object() {
  _name="${1##*/}"
  # 组装oss路径
  _oss_path=$OSS_PATH_PREFIX/$2/$_name
  echo put $file to $_oss_path
  # 上传
  _cmd="$sh_home/ossutil api put-object --bucket $BUCKET --key $_oss_path --body file://$1 -c $sh_home/account.conf"
  echo eval $_cmd
  #exit 1
  resp=$(eval $_cmd)
  echo resp $resp
}


_list_log() {
  for file in $LOG_PATH/*; do
    # 判断是否是文件
    if [ -f $file ]; then
    	_y=$(ls -l $file | awk '{print $8}')
    	_m=$(ls -l $file | awk '{print $6}')
    	_d=$(ls -l $file | awk '{print $7}')
    	#echo $file $_y $_m $_d
    	# 处理当前年：如果是时:分格式，则换为当前年
    	if echo $_y | grep -q ':'; then
    		_y=$(date +%Y)
    	fi
    	# 月份替换为数字
      _m=`echo $_m |sed 's/Jan/01/'|sed 's/Feb/02/' |sed 's/Mar/03/'|sed 's/Apr/04/'|sed 's/May/05/'|sed 's/Jun/06/'|sed 's/Jul/07/'|sed 's/Aug/08/'|sed 's/Sep/09/'|sed 's/Oct/10/'|sed 's/Nov/11/'|sed 's/Dec/12/'`
      #echo $file $_y $_m $_d
      # 比较日期
      _t=$(date -d "$_y-$_m-$_d" +%s)
      #echo _t $_t
      _now=$(date +%s)
      if [ $_t -gt $_now ]; then
        _y=$((_y - 1))
        _t=$(date -d "$_y-$_m-$_d" +%s)
      fi
      echo $_y-$_m-$_d $file, _now $_now, _t $_t, $(((_now - _t)/24/60/60))
      if [ $(((_now - _t)/24/60/60)) -gt $BEFORE_DAY ]; then        
      	# 上传
      	_put_object $file "$_y/$_m" && \
      	rm -rf $file
      fi
    fi
  done
  echo done	
}

_list_log
